<!DOCTYPE html>
<html>
<!--
Copyright 2011 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<head>
<title>Expression</title>
<link rel="stylesheet" href="common_styles.css">
<script src="include.js"></script>
<style>
.inputValue {
  color: green;
}
.computedValue {
  color: red;
}
</style>
</head>
<body>
  <div class="example">
    <h1>Expression</h1>
    <div class="content">
      <ul>
      <template iterate>
        <li>{{ name }} is
          <span class="inputValue">{{ weight }}</span>
            lbs,
          <span class="inputValue">{{ age }}</span>
            human years and
          <span class="computedValue">
            {{ expr(age @ humanYears, weight) ageInDogYears(humanYears, weight) }} dog years
          </span>
        </li>
      </template>
      </ul>
    </div>
  </div>

<script>
// Under 20 lbs = 5.06
// 20-50 = 5.53
// 50-90 = 6.2
// > 90lbs = 7.66
var weightFactors = [5.06, 5.53, 6.2, 7.66];

function ageInDogYears(humanYears, weight) {
  var factorIndex;
  if (weight > 90)
    factorIndex = 3;
  else if (weight > 50)
    factorIndex = 2;
  else if (weight > 20)
    factorIndex = 1;
  else
    factorIndex = 0;

  return humanYears * weightFactors[factorIndex];
}

document.body.model = [
  {
    name: 'Mr. Snacky Fingers',
    weight: 12,
    age: 2
  },
  {
    name: 'Bitey',
    weight: 80,
    age: 5
  },
  {
    name: 'Killey Dee Williams',
    weight: 35,
    age: 7
  }
];

document.body.modelDelegate = MDVDelegate;
</script>

  <div class="comment">
    <h1>Comment</h1>
    <div class="content">
      Often a view needs to compute an output value which isn't necessary to
      include in the model data and can be derived from existing values. The
      expr() syntax allows for an arbitrary expression (include calling
      functions in the global scope) as well as declaring (and aliasing)
      dependencies.
    </div>
  </div>
</body>
</html>